<template>
  <div class="ruslt-container">
    <!-- 1.导航栏 -->
    <van-nav-bar
      :title="$route.query.keywords + '的搜索结果'"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <!--2.加载列表  -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- 搜索结果列表 -->
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push({ path: '/articles', query: { id: item.art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
// 导入接口
import { getSeach } from "./home.js";
export default {
  name: "searchRuslt",
  data() {
    return {
      loading: false, //加载更多
      finished: false, //onload结束
      page: 1, //页码
      list: [], //搜索出来的结果
    };
  },
  methods: {
    async onLoad() {
      let res = await getSeach({
        page: this.page,
        per_page: 10,
        q: this.$route.query.keywords,
      });
      //   console.log(res);
      //   页码自增
      this.page++;
      //   绑定数据 由于数组无法直接绑定
      this.list.push(...res.data.data.results);
      //   加载完毕
      this.loading = false;
      //   判断是否加载完毕
      if (res.data.data.results.length == 0) {
        this.finished = true;
      }
    },
  },
};
</script>

<style lang="less">
* {
  // 內减
  box-sizing: border-box;
}
// 避免用户快速滑动出现重绘bug
body {
  overflow: hidden;
}
.ruslt-container {
  height: 100vh;
  padding-top: 46px;

  .van-list {
    height: 100%;
    overflow-y: scroll;
  }
}
</style>